package com.ruoyi.wechat.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.wechat.domain.MenuVo;
import com.ruoyi.wechat.domain.Result;
import com.ruoyi.wechat.utils.ConstantPropertiesUtil;
import com.ruoyi.wechat.utils.HttpClientUtils;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.wechat.domain.Menu;
import com.ruoyi.wechat.service.IMenuService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;

/**
 * 菜单列表Controller
 *
 * @author 林道敬
 * @date 2025-01-10
 */
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController
{
    @Autowired
    private IMenuService menuService;

    //获取所有菜单，按照一级和二级菜单封装
    @GetMapping("findMenuInfo")
    public Result findMenuInfo() {
        List<MenuVo> list = menuService.findMenuInfo();
        return Result.ok(list);
    }

    @GetMapping("getAccessToken")
    public Result getAccessToken() {
        //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        try {
            //拼接请求地址
            StringBuffer buffer = new StringBuffer();
            buffer.append("https://api.weixin.qq.com/cgi-bin/token");
            buffer.append("?grant_type=client_credential");
            buffer.append("&appid=%s");
            buffer.append("&secret=%s");
            //请求地址设置参数
            String url = String.format(buffer.toString(),
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            //发送http请求
            String tokenString = HttpClientUtils.get(url);
            //获取access_token
            JSONObject jsonObject = JSONObject.parseObject(tokenString);

            String access_token = jsonObject.getString("access_token");
            //返回
            return Result.ok(access_token);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(null);
        }
    }

    @GetMapping("syncMenu")
    public Result createMenu() throws WxErrorException {
        menuService.syncMenu();
        return Result.ok(null);
    }
    /**
     * 查询菜单列表列表
     */
    @RequiresPermissions("wechat:menu:list")
    @GetMapping("/list")
    public AjaxResult list(Menu menu)
    {
        List<Menu> list = menuService.selectMenuList(menu);
        return success(list);
    }

    /**
     * 导出菜单列表列表
     */
    @RequiresPermissions("wechat:menu:export")
    @Log(title = "菜单列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Menu menu)
    {
        List<Menu> list = menuService.selectMenuList(menu);
        ExcelUtil<Menu> util = new ExcelUtil<Menu>(Menu.class);
        util.exportExcel(response, list, "菜单列表数据");
    }

    /**
     * 获取菜单列表详细信息
     */
    @RequiresPermissions("wechat:menu:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(menuService.selectMenuById(id));
    }

    /**
     * 新增菜单列表
     */
    @RequiresPermissions("wechat:menu:add")
    @Log(title = "菜单列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Menu menu)
    {
        return toAjax(menuService.insertMenu(menu));
    }

    /**
     * 修改菜单列表
     */
    @RequiresPermissions("wechat:menu:edit")
    @Log(title = "菜单列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Menu menu)
    {
        return toAjax(menuService.updateMenu(menu));
    }

    /**
     * 删除菜单列表
     */
    @RequiresPermissions("wechat:menu:remove")
    @Log(title = "菜单列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(menuService.deleteMenuByIds(ids));
    }
}
